package com.axter.libs.photo.previewphoto;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.axter.libs.photo.R;

import java.util.ArrayList;

/**
 * 预览图片
 * Created by axter on 2016/11/29.
 */

public class PreviewPhotoCheckActivity extends PreviewPhotoBaseActivity {
    private ArrayList<Integer> mChecked;

    private TextView tv_title;
    private ImageView iv_check;
    private TextView tv_check;
    private LinearLayout laout_check;
    private RelativeLayout layout_bottom;
    private ImageButton back;
    private Button finish;

    @Override
    protected void receiveIntentArgs() {
        super.receiveIntentArgs();

        if (getIntent() != null && getIntent().getExtras() != null) {
            if (getIntent().getExtras().containsKey(EXTRA_CHECK)) {
                mChecked = getIntent().getExtras().getIntegerArrayList(EXTRA_CHECK);
            }
        }

        if (mChecked == null && mPaths != null && mPaths.size() > 0) {
            mChecked = new ArrayList<>(mPaths.size());
        }

        if (mChecked != null && mPaths != null && mChecked.size() != mPaths.size()) {
            for (int i = mChecked.size(), count = mPaths.size(); i < count; i++) {
                mChecked.add(0);
            }
        }
    }

    @Override
    protected int getContentView() {
        return R.layout.previewphoto_mcheck;
    }

    @Override
    protected void initView() {
        super.initView();
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_check = (ImageView) findViewById(R.id.iv_check);
        tv_check = (TextView) findViewById(R.id.tv_check);
        laout_check = (LinearLayout) findViewById(R.id.laout_check);
        layout_bottom = (RelativeLayout) findViewById(R.id.layout_bottom);
        back = (ImageButton) findViewById(R.id.back);
        finish = (Button) findViewById(R.id.finish);
        finish.setEnabled(true);

        back.setOnClickListener(this);
        finish.setOnClickListener(this);
        laout_check.setOnClickListener(this);
    }

    @Override
    public void setPageText(int position) {
        tv_title.setText(position + 1 + "/" + getPagerCount());

        if (mChecked.get(position) == 0) {
            iv_check.setSelected(false);
        } else {
            iv_check.setSelected(true);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        } else if (v.getId() == R.id.finish) {
            finishCheck();
        } else if (v.getId() == R.id.laout_check) {
            if (mChecked.get(getCurrentItem()) == 0) {
                mChecked.set(getCurrentItem(), 1);
                iv_check.setSelected(true);
            } else {
                mChecked.set(getCurrentItem(), 0);
                iv_check.setSelected(false);
            }
        }
    }

    public void finishCheck() {
        if (mChecked != null && mChecked.size() > 0) {
            Intent intent = new Intent();
            intent.putIntegerArrayListExtra(EXTRA_OUT_CHECK, mChecked);
            setResult(Activity.RESULT_OK, intent);
        }
        finish();
    }
}
